//
//  XLDoctor
//
//  Created by longma on 2018/7/31.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "MJPhotoHelper.h"
@interface MJPhotoHelper ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation MJPhotoHelper

/**
 单例
 */
+ (instancetype)shared{
    static MJPhotoHelper *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[MJPhotoHelper alloc] init];
    });
    return instance;
}

- (void)getSelectPhotoWithBlock:(HandleImageBlock)block {
    _block = block;

    
    [self jumpPhotoLibrary];

    
//    MJWeakSelf
//    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction * camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [weakSelf jumpCamera];
//    }];
//    [alertController addAction:camera];
//    
//    UIAlertAction * photoLibrary = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [weakSelf jumpPhotoLibrary];
//    }];
//    
//    [alertController addAction:photoLibrary];
//    UIAlertAction * cancle = [UIAlertAction actionWithTitle:MJUILocalizedString(@"Cancel_qx") style:UIAlertActionStyleCancel handler:nil];
//    [alertController addAction:cancle];
//    UIViewController * viewcontroller = [UIViewController mj_topViewController];
//    [viewcontroller presentViewController:alertController animated:YES completion:nil];
}

-(void)jumpCamera{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
        pickVC.videoQuality = UIImagePickerControllerQualityTypeMedium;//可以选择图片质量
        pickVC.delegate = self;
        pickVC.allowsEditing = YES;
        pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        [[UIViewController mj_topViewController] presentViewController:pickVC animated:true completion:nil];
    }
    else{
        NSLog(@"MJLog=======================相机调用失败");
    }
}

-(void)jumpPhotoLibrary
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        [[UINavigationBar appearance] setTintColor:UIColor.blackColor];

        UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
        pickVC.delegate = self;
        pickVC.allowsEditing = YES;
        pickVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [[UIViewController mj_topViewController] presentViewController:pickVC animated:true completion:nil];
    }else{
        
        MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
        configure.confirmButtonColor = UIColor.mj_appColor;
        configure.confirmTitle = MJUILocalizedString(@"Yes");
        configure.cancelTitle = MJUILocalizedString(@"Cancel_qx");
        configure.itemType = 1;
        configure.rightButtonTheme = UIColor.mj_appColor;
        configure.leftButtonTheme = UIColor.mj_grayColor;
        [MJUIAlertView alertViewWithTitle:nil message:MJUILocalizedString(@"Please enable Allow access to albums") configure:configure confirmBlock:^{
            
        } cancelBlock:^{
           
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSLog(@"image:%@",info);
    UIImage *image = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
    if (_block) {
        _block(image);
    }
}



@end
